/**
 * 导出批注
 */
function cleanWhite(val) {
  return val.replace(/\s*/g, '')
}
const Comments = {
  WdInformation: {
    wdActiveEndPageNumber: 3,
    wdFirstCharacterLineNumber: 10
  },
  parseComments: () => {
    const doc = wps.WpsApplication().ActiveDocument;

    const filePath = doc.FullName;
    const comments = doc.Comments;
    if (comments.Count === 0) {
      alert('文档没有批注');
      return null;
    }
    const res = [filePath];
    for (let i = 1; i < comments.Count + 1; i++) {
      const c = comments.Item(i);

      console.log('df', c);


      const commentObject = {
        pagenumber: c.Scope.Information(
          Comments.WdInformation.wdActiveEndPageNumber
        ),
        linenumber: c.Scope.Information(
          Comments.WdInformation.wdFirstCharacterLineNumber
        ),
        src: c.Scope.Text,
        comment: c.Range.Text,
        author: c.Author
      };
      res.push(commentObject);
    }
    return res;
  },
  writeToDoc: (comments) => {
    const newDoc = wps.WpsApplication().Documents.Add();
    newDoc.Content.ParagraphFormat.CharacterUnitFirstLineIndent = 2;
    newDoc.Content.Paragraphs.Item(1).Range.Font.Size = 16;
    newDoc.Content.Paragraphs.Item(1).Range.Font.Name = '方正仿宋_GBK';
    newDoc.Content.Paragraphs.Item(1).Range.Font.NameAscii = 'Times New Roman';

    const par = newDoc.Content.Paragraphs.Add();
    par.Range.Text = '导出批注工具';
    par.Range.InsertParagraphAfter();

    const fileName = comments.shift();
    par.Range.InsertAfter('以下是来自文档“' + fileName + '”的批注：');
    par.Range.InsertParagraphAfter();

    par.Range.InsertAfter(
      '------------------------分割线------------------------'
    );
    par.Range.InsertParagraphAfter();

    for (let i = 0; i < comments.length; i++) {
      const element = comments[i];
      par.Range.InsertAfter(
        `${i + 1}、第${element.pagenumber}页，第${element.linenumber}行 || ` +
        `原文：${element.src} || 意见：${element.comment}`
      );
    }
  },
  writeToSheet: (comments) => {
    const doc = wps.WpsApplication().ActiveDocument;
    const currentPath = doc.Path
    const rows = comments.slice(1).map(v => {
      return `\"${cleanWhite(v.src)}\",\"${cleanWhite(v.comment)}\"`
    }).join('\n')


    try {
      wps.FileSystem.AppendFile(`${currentPath}/意见.csv`, rows)

    } catch (error) {
      console.error(error);
    }


  },
  getComments: () => {
    const comments = Comments.parseComments();
    if (comments) {
      Comments.writeToDoc(comments);
    }
  },

  getTableComments: () => {
    const comments = Comments.parseComments();
    console.log('table com2', comments);

    if (comments) {
      Comments.writeToSheet(comments)

    }
  }
};
